package deployr.core;

import static java.text.MessageFormat.*;

import java.io.*;

public class Files {

	public static void deleteIfExist(final String path) {
		final File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
	}

	public static final void mkdirUnlessExists(String path) {
		if (!exists(path)) mkdir(path);
	}

	public static final void mkdirUnlessExists(File file) {
		mkdirUnlessExists(file.getAbsolutePath());
	}

	public static final boolean exists(final String path) {
		return new File(path).exists();
	}

	public static final boolean exists(final String path, final String otherPath) {
		return exists(format("{0}/{1}", path, otherPath));
	}

	public static boolean isDirectory(final String relativePath, final String root) {
		final File item = new File(root, relativePath);
		if (item.isDirectory()) return true;
		return false;
	}

	public static void mkdir(final String path) {
		File file = new File(path);
		file.mkdirs();
	}

	public static final String srcTest(final String path) {
		return userDir("src/test").concat("/").concat(path);
	}

	public static final String target(final String path) {
		return "target".concat("/").concat(path);
	}

	public static String testResources(final String path) {
		return format("src/test/resources/{0}", path);
	}

	public static final String userDir(final String path) {
		return System.getProperty("user.dir").concat("/").concat(path);
	}

}
